package com.example.layui.controller;

import com.example.layui.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;

@Controller
public class LoginController {

    @Resource
    private UserService userService;

    @RequestMapping("/login")
    public String login(){
        return "/admin/login/login";
    }

    @RequestMapping("/dologin")
    public String dologin(String userNumber, String password){
        try {
            userService.login(userNumber, password);
            return "redirect:/main";
        }catch (AuthenticationException e){
            return "login";

        }
    }

    @RequestMapping("/403")
    public String forbidden(){
        return "403";
    }
    @RequestMapping(value = {"/logout"}, method = RequestMethod.GET)
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:login";
    }
}
